package com.menhai.personmanagement.services;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.MbPopulation;
import com.menhai.personmanagement.entity.TjInspectphysical;
import com.menhai.personmanagement.entity.User;
import com.menhai.personmanagement.entity.co.TjInspectphysicalCo;
import com.menhai.personmanagement.entity.co.TjSkCo;
import com.menhai.personmanagement.entity.vo.TjInspectphysicalVo;
import com.menhai.personmanagement.entity.vo.TjInspectphysicalfrontVo;
import com.menhai.personmanagement.mapper.MbPopulationMapper;
import com.menhai.personmanagement.mapper.TjInspectphysicalMapper;
import com.menhai.personmanagement.mapper.UserMapper;
import com.menhai.personmanagement.plus.DateUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author shikun
 * @since 2022-01-12
 */
 @Service
public class ITjInspectphysicalService{


     @Autowired
     private TjInspectphysicalMapper tjnspectphysicalMapper;

     @Autowired
     private MbPopulationMapper mbPopulationMapper;

     @Autowired
     private UserMapper userMapper;

     public int save(String level,TjInspectphysical tjInspectphysical,String account){
         QueryWrapper wrapper = new QueryWrapper();
         wrapper.eq("superiorCode", level);
         wrapper.eq("jobnumber", account);
         User user=userMapper.selectOne(wrapper);

         tjInspectphysical.setHosnum(level);
         if(null != user && null != user.getId()) {
             tjInspectphysical.setUserid(user.getId());
         }

         tjInspectphysical.setInspectTime(DateUtils.getLocalTime());
         return tjnspectphysicalMapper.insert(tjInspectphysical);
     }

     public int addProposalSubmit(TjInspectphysical tjInspectphysical){
         tjInspectphysical.setState(1);
         tjInspectphysical.setProposalTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
         return tjnspectphysicalMapper.updateById(tjInspectphysical);
     }

     public int getPageTotal(String level,String name){
         Date curr = new Date();
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         String temp = format.format(curr)+"%";
         int total = tjnspectphysicalMapper.getPageTotal(level,temp,name);
         return total;
     }

     public List getAllInspectByUserName(String name,String level,int pagesize,int pagenum){
         Date curr = new Date();
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         String temp = format.format(curr)+"%";
         return tjnspectphysicalMapper.getAllInspectByUserName(name, level+"%", temp, pagesize, (pagenum-1)*pagesize);
     }

     public int updateInspectInfo(TjInspectphysical tjInspectphysical){
         tjInspectphysical.setState(1);
         Date curr = new Date();
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String temp = format.format(curr);
         tjInspectphysical.setProposalTime(temp);
         return tjnspectphysicalMapper.updateById(tjInspectphysical);
     }

     public TjInspectphysical findInspectInfoById(int id){
         return tjnspectphysicalMapper.selectById(id);
     }

     public List findHistoryByPeopleId(int id,String hosnum){
         return tjnspectphysicalMapper.findHistoryByPeopleId(id,hosnum+"%");
     }

     public List findHistoryByDate(int tian,String date1,String date2,int mbpeopleId, String mbhosnum){
         if(tian>=0){
             return tjnspectphysicalMapper.findHistoryByDate(tian,mbpeopleId,mbhosnum);
         }else {
             QueryWrapper wrapper = new QueryWrapper();
             if (null != date1 && !date1.equals("")) {
                 wrapper.ge("createTime", date1);
             }
             if (null != date2 && !date2.equals("")) {
                 wrapper.le("createTime", date2);
             }
             wrapper.eq("mbpeopleId", mbpeopleId);
             wrapper.eq("hosnum", mbhosnum);
             return tjnspectphysicalMapper.selectList(wrapper);
         }
     }

    /**
     * @describe 体检首页
     * @param superiorCode
     * @Return com.menhai.personmanagement.entity.vo.TjInspectphysicalfrontVo
     * @Date 2022/1/12 15:40
     * @author shikun
     */
    public TjInspectphysicalfrontVo bodycheckupfront(String superiorCode){
        QueryWrapper<MbPopulation> filterquery = new QueryWrapper<>();
        filterquery.likeRight("superiorCode",superiorCode);
        int totalpeople =mbPopulationMapper.selectCount(filterquery);//总人数

        int bodycheckup = tjnspectphysicalMapper.bodycheckupnumber(superiorCode);//体检总人数

        int sixtycheckup = tjnspectphysicalMapper.sixtycheckupnumber(superiorCode);//60岁以上

        int eighteenarrivesixty = tjnspectphysicalMapper.eighteenarrivesixtynumber(superiorCode);//18到60岁

        int eighteenunder = tjnspectphysicalMapper.eighteenundernumber(superiorCode);//18以下

        int nowadays = tjnspectphysicalMapper.nowadaysnumber(superiorCode);//今日体检人次

        int thisweek = tjnspectphysicalMapper.thisweeknumber(superiorCode);//本周体检人次

        int thismonth = tjnspectphysicalMapper.thismonthnumber(superiorCode);//本月体检人次

        return TjInspectphysicalfrontVo.builder()
                .totalpeople( String.valueOf(totalpeople))
                .bodycheckup(String.valueOf(bodycheckup))
                .sixtycheckup(String.valueOf(sixtycheckup))
                .eighteenarrivesixty(String.valueOf(eighteenarrivesixty))
                .eighteenunder(String.valueOf(eighteenunder))
                .nowadays(String.valueOf(nowadays))
                .thisweek(String.valueOf(thisweek))
                .thismonth(String.valueOf(thismonth))
                .total(String.valueOf(bodycheckup))
                .build();
    }

    /**
     * @describe 体检首页 年龄分类
     * @param superiorCode
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.TjInspectphysicalfrontVo>
     * @Date 2022/1/13 10:37
     * @author shikun
     */
    public List<TjInspectphysicalfrontVo> bodydetectagelayered(String superiorCode) {
        return tjnspectphysicalMapper.bodydetectagelayered(superiorCode);
    }

    /**
     * @describe 体检首页 性别
     * @param superiorCode
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.TjInspectphysicalfrontVo>
     * @Date 2022/1/13 10:53
     * @author shikun
     */
    public List<TjInspectphysicalfrontVo> bodydetesex(String superiorCode) {
        return tjnspectphysicalMapper.bodydetesex(superiorCode);
    }

    /**
     * @describe 每日体检数量
     * @param superiorCode
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.TjInspectphysicalfrontVo>
     * @Date 2022/1/13 11:23
     * @author shikun
     */
    public List<TjInspectphysicalfrontVo> dailybodycheck(String superiorCode) {
        return tjnspectphysicalMapper.dailybodycheck(superiorCode);
    }

    public List findHistoryByPeopleId(int id){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("mbpeopleId", id);
        return tjnspectphysicalMapper.selectList(wrapper);
    }

    public List<Map<String,Object>> getAllInspect(String hosnum,int pagesize,int pagenum){
        Date curr = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return tjnspectphysicalMapper.getAllInspect(hosnum,pagesize, (pagenum-1) * pagesize, format.format(curr)+"%");
    }


    public Page exprotDiseaserisk(String level,int pagesize,int pagenum, String name, String risk, boolean flag){

        if(flag){

        }else{

        }
        return null;
    }

    /**
     * @describe 每日工作量统计表
     * @param tjSkCo
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.menhai.personmanagement.entity.vo.TjInspectphysicalVo>
     * @Date 2022/2/23 11:55
     * @author shikun
     */
    public IPage<TjInspectphysicalVo> selectBycreateTime(TjSkCo tjSkCo) {
        Page<TjInspectphysicalVo> page = new Page<>(tjSkCo.getPageIndex(), tjSkCo.getPageSize());
        if(tjSkCo.getHosnum().length()==8){
            QueryWrapper jobnumber = new QueryWrapper<>();
            jobnumber.eq("jobnumber",tjSkCo.getAccount());
            List<User> jobnumberList = userMapper.selectList(jobnumber);
            if(!jobnumberList.isEmpty()){
                tjSkCo.setUserid(jobnumberList.get(0).getId());
            }
        }
        return tjnspectphysicalMapper.selectBycreateTime(page,tjSkCo);
    }

    /**
     * @describe 每日工作量统计表导出
     * @param tjSkCo
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.TjInspectphysicalVo>
     * @Date 2022/2/23 13:16
     * @author shikun
     */
    public List<TjInspectphysicalVo> exportBycreateTime(TjSkCo tjSkCo) {
        if(tjSkCo.getHosnum().length()==8){
            QueryWrapper jobnumber = new QueryWrapper<>();
            jobnumber.eq("jobnumber",tjSkCo.getAccount());
            List<User> jobnumberList = userMapper.selectList(jobnumber);
            if(!jobnumberList.isEmpty()){
                tjSkCo.setUserid(jobnumberList.get(0).getId());
            }
        }
        return tjnspectphysicalMapper.exportBycreateTime(tjSkCo);
    }

    /**
     * @describe 每月工作量统计表
     * @param tjSkCo
     * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.menhai.personmanagement.entity.vo.TjInspectphysicalVo>
     * @Date 2022/2/23 16:38
     * @author shikun
     */
    public IPage<TjInspectphysicalVo> selectBymonthgrouping(TjSkCo tjSkCo) {
        Page<TjInspectphysicalVo> page = new Page<>(tjSkCo.getPageIndex(), tjSkCo.getPageSize());
        if(tjSkCo.getCreateTime()==null||tjSkCo.getCreateTime().equals("")){
            Calendar a=Calendar.getInstance();
            tjSkCo.setCreateTime(String.valueOf(a.get(Calendar.YEAR)));
        }
        if(tjSkCo.getHosnum().length()==8){
            QueryWrapper jobnumber = new QueryWrapper<>();
            jobnumber.eq("jobnumber",tjSkCo.getAccount());
            List<User> jobnumberList = userMapper.selectList(jobnumber);
            if(!jobnumberList.isEmpty()){
                tjSkCo.setUserid(jobnumberList.get(0).getId());
            }
        }
        return tjnspectphysicalMapper.selectBymonthgrouping(page,tjSkCo);
    }


    /**
     * @describe 每月工作量统计表导出
     * @param tjSkCo
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.TjInspectphysicalVo>
     * @Date 2022/2/24 9:09
     * @author shikun
     */
    public List<TjInspectphysicalVo> exportBymonthgrouping(TjSkCo tjSkCo) {
        if(tjSkCo.getCreateTime()==null||tjSkCo.getCreateTime().equals("")){
            Calendar a=Calendar.getInstance();
            tjSkCo.setCreateTime(String.valueOf(a.get(Calendar.YEAR)));
        }
        if(tjSkCo.getHosnum().length()==8){
            QueryWrapper jobnumber = new QueryWrapper<>();
            jobnumber.eq("jobnumber",tjSkCo.getAccount());
            List<User> jobnumberList = userMapper.selectList(jobnumber);
            if(!jobnumberList.isEmpty()){
                tjSkCo.setUserid(jobnumberList.get(0).getId());
            }
        }
        return tjnspectphysicalMapper.exportBymonthgrouping(tjSkCo);
    }

    public List<TjInspectphysicalVo> finishbodycheckuplist(TjInspectphysicalCo tjInspectphysicalCo) {
        return tjnspectphysicalMapper.finishbodycheckuplist(tjInspectphysicalCo);
    }


    public int insertReport(String data){
        List<TjInspectphysical> list = new ArrayList<>();
        JSONArray json = new JSONArray(data);
        List<TjInspectphysical> l = tjnspectphysicalMapper.getReports(new JSONObject(json.get(0)).get("peopleId").toString());
        for (int i = 0; i < json.size(); i++) {
            TjInspectphysical t = new TjInspectphysical();
            JSONObject o = new JSONObject(json.get(i));
            t.setReportId(o.get("rid").toString());
            t.setMbpeopleId(Integer.parseInt(o.get("peopleId").toString()));
            t.setHosnum(o.get("hosnum").toString());
            t.setInspectTime(o.get("createTime").toString());
            list.add(t);
        }
        int temp1 = list.size();
        for (int i = 0; i < l.size(); i++) {
            for (int i1 = 0; i1 < list.size(); i1++) {
                if(list.get(i1).getReportId().equals(l.get(i).getReportId())){
                    list.remove(i1);
                }
            }
        }
        int temp2 = list.size();
        if(temp2 ==0 &&temp1 >0){
            return 1;
        }
        return tjnspectphysicalMapper.batchInsert(list);
    }

    /**
     * 查询中医体检记录列表
     * @param
     * @return
     */
    public IPage<TjInspectphysicalCo> getInspectPhysicalList(TjInspectphysicalCo tjInspectphysicalCo){
        Page<TjInspectphysicalCo> page = new Page<>(tjInspectphysicalCo.getPageIndex(),tjInspectphysicalCo.getPageSize());
        return tjnspectphysicalMapper.getInspectPhysicalList(page,tjInspectphysicalCo);
    }

    /**
     * 左侧树
     * @param hosnum
     * @return
     */
    public List<Map<String,Object>> getCunRenShu(String hosnum){
        return tjnspectphysicalMapper.getCunRenShu(hosnum);
    }

}
